package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.ConversationTable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author system
 * @since 2024-10-23 22:27:03
 */
@Component
public class ConversationTableSpecifications {

    public static Specification<ConversationTable> queryList(ConversationTable conversationTable) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (conversationTable.getConversationId() != null) {
                predicates.add(builder.like(root.get("conversationId" ).as(String.class), "%" + conversationTable.getConversationId() + "%" ));
            }
            if (conversationTable.getFeedbackId() != null) {
                predicates.add(builder.equal(root.get("feedbackId" ).as(Integer.class), conversationTable.getFeedbackId()));
            }
            if (conversationTable.getSenderId() != null) {
                predicates.add(builder.equal(root.get("senderId" ).as(Integer.class), conversationTable.getSenderId()));
            }
            if (conversationTable.getSenderType() != null) {
                predicates.add(builder.like(root.get("senderType" ).as(String.class), "%" + conversationTable.getSenderType() + "%" ));
            }
            if (conversationTable.getSentTime() != null) {
                predicates.add(builder.equal(root.get("sentTime" ).as(Date.class), conversationTable.getSentTime()));
            }
            if (conversationTable.getMessageContent() != null) {
                predicates.add(builder.like(root.get("messageContent" ).as(String.class), "%" + conversationTable.getMessageContent() + "%" ));
            }
            if (conversationTable.getIsRead() != null) {
                predicates.add(builder.like(root.get("isRead" ).as(String.class), "%" + conversationTable.getIsRead() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
